package com.myspace.backend.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.myspace.backend.mapper.PostMapper;
import com.myspace.backend.pojo.Post;
import com.myspace.backend.service.user.UserPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserPostServiceImpl implements UserPostService {

    @Autowired
    private PostMapper postMapper;

    @Override
    public List<Map<String, String>> getUserpost(String userId) {

        List<Map<String, String>> list = new ArrayList<>();
        int id = Integer.parseInt(userId);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        List<Post> posts = postMapper.selectList(queryWrapper);

        for (Post post: posts) {
            Map<String, String> map = new HashMap<>();
            map.put("id", post.getId().toString());
            map.put("userId", post.getUserId().toString());
            map.put("content", post.getContent());
            list.add(map);
        }

        return list;
    }
}
